package com.jiyun.gateway.filter;

import com.alibaba.cloud.commons.lang.StringUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import utils.JwtUtil;

@Component
@Slf4j
public class MyFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String loginPageUrl = "/user/login";
        String tupian="course/tupian";
        String tupian2="contest/tupian";
        String bioti="shiti/biaoti";
        String daoru="shiti/daoru";
        String daochu="shiti/daochu";
        log.info("执行网关");
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String path = request.getURI().getPath();
        //log.info("url是："+path);
        log.info("url是：{}",path);
        //知道当前的URL是不是登录的URL
        //1：只有登录的情况下， 才能访问页面  2：访问页面的时候或者api,access_token（访问令牌）

        //鉴权(简单)
        if (path.contains(tupian)){
            log.info("图片，直接放行");
            return chain.filter(exchange);
        }
        if (path.contains(daoru)){
            log.info("图片，直接放行");
            return chain.filter(exchange);
        }
        if (path.contains(daochu)){
            log.info("图片，直接放行");
            return chain.filter(exchange);
        }
        if (path.contains(bioti)){
            log.info("图片，直接放行");
            return chain.filter(exchange);
        }
        if (path.contains(tupian2)){
            log.info("图片，直接放行");
            return chain.filter(exchange);
        }
        if(path.contains(loginPageUrl)){
            log.info("本次访问的是登陆页面,直接放行");
            return chain.filter(exchange);
        }else {
            log.info("本次访问的不是登陆页面，是其他页面");
            String token = getToken(request);
            //如果token没有值， 代表之前没有登录过，
            if(StringUtils.isBlank(token)){
                log.info("重定向到登陆页面");
                //状态：没有权限  401
                response.setStatusCode(HttpStatus.UNAUTHORIZED);
                return response.setComplete();

            }else{
                    Claims claimsBody = JwtUtil.getClaimsBody(token);
                    int i = JwtUtil.verifyToken(claimsBody);
                    if(i==-1||i==0){
                        log.info("放行");
                        return chain.filter(exchange);
                    }else {
                        response.setStatusCode(HttpStatus.UNAUTHORIZED);
                        return response.setComplete();
                    }


            }



        }

    }

    private String getToken(ServerHttpRequest request) {
        String token = request.getHeaders().getFirst("token");
        log.info("token:{}",token);
        return token;
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
